package providev.learn.imagina.tema2;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;

public class EditTextModified extends EditText{
    //Constructor requerido para crear el objeto View mediante codigo
    public EditTextModified(Context context) {
            super(context);
    }
    //Constructor requerido para crear el objeto View mediante codigo
    public EditTextModified(Context context, AttributeSet ats){
            super(context, ats);
    }
    //Constructor requerido para crear el objeto View mediante XML
    public EditTextModified(Context context, AttributeSet ats, int defaultStyle){
            super(context, ats, defaultStyle);
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
            if(hasFocus()){
                    CharSequence text = getText();
                    //Se comprueba si la tecla pulsada es un número
                    if(keyCode != KeyEvent.KEYCODE_0 &&
                                    keyCode != KeyEvent.KEYCODE_1 &&
                                    keyCode != KeyEvent.KEYCODE_2 &&
                                    keyCode != KeyEvent.KEYCODE_3 &&
                                    keyCode != KeyEvent.KEYCODE_4 &&
                                    keyCode != KeyEvent.KEYCODE_5 &&
                                    keyCode != KeyEvent.KEYCODE_6 &&
                                    keyCode != KeyEvent.KEYCODE_7 &&
                                    keyCode != KeyEvent.KEYCODE_8 &&
                                    keyCode != KeyEvent.KEYCODE_9 &&
                                    keyCode != KeyEvent.KEYCODE_DEL){
                            if(text.length() > 0){
                                    text = text.subSequence(0, text.length() - 1);
                                    setText(text.toString());
                                    setSelection(text.length());
                                    
                            }
                    }
            }
            return super.onKeyUp(keyCode, keyEvent);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.setMeasuredDimension(300, 60);
    }
}